<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>用户注册</title>
<style type="text/css">
<!--
td {  font-size: 9pt}
-->
</style>
</head>
<body bgcolor="#ffffff" topmargin=0>

<?php

include 'config.php';
include_once ("sendmail.php");

function check_username($username)
{
	return 1;
}

function check_password($password)
{
	return 1;
}

function check_email_address($val)
{
	return 1;
}

function generate_activate_code()
{
	    $str = substr(md5(time()), 0, 8);
	        return $str;
}

function send_activation_email($email, $username, $code)
{
	$subject = "欢迎注册西湖网络VPN帐号";
	$content = "<p>亲爱的 $username:</p>

		<p>感谢您注册西湖网络VPN帐号!</p>

		<p>请点击下面的链接激活您的帐号：</p>
		<a href=\"http://www.thewestlake.net/activate.php?userid=$username&code=$code\">http://www.thewestlake.net/activate.php?userid=$username&code=$code</a>

		<p><p>
		<p> &nbsp; 2015 &copy; <a href=\"http://www.thewestlake.net/\">西湖网络</a>
		";
	return send_email($email, $subject, $content);
}

if (isset($_POST['submit'])) {
	$username = $_POST['username'];
	$password = $_POST['password'];
	$password2 = $_POST['password2'];
	$email = $_POST['email'];
	$refid = $_POST['refid'];
	$userinfo = $configValues['CONFIG_DB_TBL_DALOUSERINFO'];
	$group = "daloRADIUS-Disabled-Users";

	if ( !check_username($username)) {
		echo "<br/><br/>
		Invalid username	<br/>";
		echo '<a href="/">返回主页</a>';
		exit;
	}
	if ( !check_password($password)) {
		echo "<br/><br/>
		Invalid password	<br/>";
		echo '<a href="/">返回主页</a>';
		exit;
	}
	if ( !check_email_address($email)) {
		echo "<br/><br/>
		Invalid email	<br/>";
		echo '<a href="/">返回主页</a>';
		exit;
	}

	include 'opendb.php';

	/* check if user already exists */
	$sql = "SELECT id, username FROM $userinfo WHERE username = '$username' or email = '$email'";
	$res = $dbSocket->query($sql);
	if ($res->numRows() >= 1){
		echo "<br/><br/>
		此用户名已经被人使用，请换一个试试。<br/>";
		$dbSocket->disconnect();
		echo '<a href="/">返回主页</a>';
		exit;
	}

	/* check if the referal user exists */
	$sql = "SELECT id, username FROM $userinfo WHERE username = '$refid'";
	$res = $dbSocket->query($sql);
	if ($res->numRows() == 0){
		echo "<br/><br/>
		邀请人ID不存在，请确认后再试。<br/>";
		$dbSocket->disconnect();
		echo '<a href="/">返回主页</a>';
		exit;
	}

	/* adding the user to the radcheck table */
	$sql = "INSERT INTO radcheck values (0, '$username', 'Cleartext-Password', ':=', '$password')";
	$res = $dbSocket->query($sql);

	/* adding user information to the userinfo table */
	$currDate = date('Y-m-d H:i:s');
	$ip = $_SERVER["REMOTE_ADDR"];
	$code = generate_activate_code(); 
	$sql = "INSERT INTO userinfo (username, email, portalloginpassword, enableportallogin, creationdate, creationby, activationcode, refid) values ('$username', '$email', '$password', '1', '$currDate', '$ip', '$code', '$refid')";
	$res = $dbSocket->query($sql);

	/* adding the user to the default group defined */
	$sql = "INSERT INTO radusergroup values ('$username', '$group', '0')";
	$res = $dbSocket->query($sql);

	$dbSocket->disconnect();

	// TODO: send activation email
	$errmsg = send_activation_email($email, $username, $code);
	if($errmsg != ""){
		echo "出错啦！激活邮件没有发送成功。请报告管理员。<br/>";
		echo "错误：$errmsg<br/>";
		echo '<a href="/">返回主页</a>';
		exit;
	}

	echo "<br/><br/>激活邮件已经发送到 ". $email . "<br/>请点击邮件中的链接激活帐号。<br/>";
	echo '<a href="/">返回主页</a>';

	exit;
} // if submit
else{
	echo "<br/><br/>No action.<br/>";
	echo '<a href="/">返回主页</a>';
	exit;
}
?>
</body>

